//
// Created by shen on 2024/6/22.
//

#include "TriangleSample.h"
#include "../util/GLUtils.h"
#include "LogUtil.h"
TriangleSample::TriangleSample() {


}
TriangleSample::~TriangleSample()  {



}
void TriangleSample::Init() {


    if (m_ProgramObj!=0){
        return;
    }

    char vShaderStr[] =
            "#version 300 es\n"
            "layout(location = 0) in vec4 vPosition;\n"
            "in vec3 color;\n"
            "out vec3 outcolor;\n"
            "void main()\n"
            "{\n"
            "   gl_Position = vPosition;\n"
            "   outcolor=color;\n"
            "}";


    char fShaderStr[] =
            "#version 300 es\n"
            "precision mediump float;\n"
            "out vec4 fragColor;\n"
            "in vec3 outcolor;\n"
            "void main()\n"
            "{\n"
            "   fragColor = vec4 (1.0,0.4,0.9,1.0 );\n"
            "}\n";


    m_ProgramObj=GLUtils::CreateProgram(vShaderStr,fShaderStr,m_VertexShader,m_FragmentShader);



}

void TriangleSample::Draw(int screenW, int screenH) {
    LOGCATE("TriangleSample::Draw");
    GLfloat vVertices[] = {
            0.0f,  0.5f, 0.5f,
            -1.0f, -0.5f, 1.0f,
            0.5f, -1.0f, 0.8f,
    };

    if(m_ProgramObj == 0)
        return;

    glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glClearColor(1.0, 1.0, 1.0, 1.0);

    glUseProgram(m_ProgramObj);

    glVertexAttribPointer(0,3,GL_FLOAT,
                          GL_FALSE,0,vVertices);

    glEnableVertexAttribArray (0);

    float timeValue = 10;
    float greenValue = sin(timeValue) / 2.0f + 0.5f;
    GLuint m_VaoIds[2];

    glGenBuffers(2,m_VaoIds);

    int vertexColorLocation = glGetAttribLocation(m_ProgramObj, "color");
    glUniform3f(vertexColorLocation, 0.8f, 1.0, 1.0f);

    glDrawArrays (GL_TRIANGLES, 0, 3);

    glUseProgram (GL_NONE);

}


void TriangleSample::Destroy() {

    if (m_ProgramObj)
    {
        glDeleteProgram(m_ProgramObj);
        m_ProgramObj = GL_NONE;
    }
}